<template>
	<div class="system-dept-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<!-- 功能栏 -->
			<div class="system-dept-search mb15">
				<el-button size="default" type="primary" class="ml10" @click="clickExpand">
					<el-icon><ele-Sort /></el-icon>
					{{ expand.state ? "全部关闭" : "全部展开" }}
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddDept('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增部门
				</el-button>
			</div>
			<!-- Table -->
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" row-key="deptId"
				:key="expand.key" :default-expand-all="expand.state"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
				<el-table-column prop="deptName" label="部门名称" show-overflow-tooltip> </el-table-column>
				<el-table-column label="排序" show-overflow-tooltip width="80">
					<template #default="scope">
						{{ scope.$index }}
					</template>
				</el-table-column>
				<el-table-column prop="status" label="部门状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 0">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="部门描述" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createdTime" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditDept('add', scope.row)">新增</el-button>
						<el-button size="small" text type="primary"
							@click="onOpenEditDept('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" :disabled="scope.row.deptId === 1"
							@click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<DeptDialog ref="deptDialogRef" @refresh="formSubmit" />
	</div>
</template>

<script setup lang="ts" name="systemDept">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { deptApi } from '/@/api/system/dept/index';
import { isEmpty, tansParams } from '/@/utils/mms';
// 引入 api 请求接口
const baseApi = deptApi();
// 展开/合闭
const expand = ref({
	state: true,
	key: 0
});


// 引入组件
const DeptDialog = defineAsyncComponent(() => import('/@/views/system/dept/dialog.vue'));

// 定义变量内容
const deptDialogRef = ref();
const state = reactive<SysDeptState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});

// 展开或折叠全部的方法
const clickExpand = () => {
	expand.value.key = +new Date();
	expand.value.state = !expand.value.state;
}

// 接受子组件传值
const formSubmit = (row: RowDeptType) => {

	if (isEmpty(row.deptId)) {
		//新增
		baseApi.insert(row).then(row => {
			deptDialogRef.value.closeDialog();
			ElMessage.success(row.msg)
			setTimeout(() => {
				getTableData();
			}, 1000)
		}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
	} else {
		//更新
		baseApi.edit(row).then(row => {
			deptDialogRef.value.closeDialog();
			ElMessage.success(row.msg)
			setTimeout(() => {
				getTableData();
			}, 1000)
		}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
	}

}

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	state.tableData.data = [];
	baseApi.queryTreeAll().then(res => {
		state.tableData.data = res.data;
		state.tableData.total = res.total;
		state.tableData.loading = false;
	}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
};
// 打开新增菜单弹窗
const onOpenAddDept = (type: string) => {
	baseApi.query(1).then(res => {
		deptDialogRef.value.openDialog(type, res.data);
	}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
};
// 打开编辑菜单弹窗
const onOpenEditDept = (type: string, row: DeptTreeType) => {
	baseApi.query(row.deptId).then(res => {
		deptDialogRef.value.openDialog(type, res.data);
	}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
};
// 删除当前行
const onTabelRowDel = (row: DeptTreeType) => {
	ElMessageBox.confirm(`此操作将永久删除部门：${row.deptName}, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			baseApi.delete(row.deptId).then(res => {
				getTableData();
				ElMessage.success('删除成功');
			}).catch(async err => { ElMessage.warning(err); }).finally(() => { })
		})
		.catch(() => { });
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
